package com.net.dao;

import java.sql.SQLException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.net.domain.ClassHours;
import com.net.domain.Student;
import com.net.domain.StudentAttendanceDetail;
import com.net.web.form.AttendanceSearchForm;

public class AttendanceDao {
	
	final static Logger log = Logger.getLogger(StudentDao.class);
	
	private SqlMapClient sqlMapClient;

	public SqlMapClient getSqlMapClient() {
		return sqlMapClient;
	}

	@Autowired
	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}
	
	@SuppressWarnings("unchecked")
	public List<StudentAttendanceDetail> getAllEligibleStudents (AttendanceSearchForm searchQuery) {
		List<StudentAttendanceDetail> studentList = null;
		
		try {
			
			if(searchQuery.getSubjectClassGroupId() > 0)
				studentList = (List<StudentAttendanceDetail>)sqlMapClient.queryForList("getEligibleStudentsByGroup", searchQuery);
			else
				studentList = (List<StudentAttendanceDetail>)sqlMapClient.queryForList("getEligibleStudents", searchQuery);
		}
		catch (Exception e) {
			log.error("Error caught while retrieving Student details" + e.getMessage(), e);
		}
		
		return studentList;
	}
	@SuppressWarnings("unchecked")
	public List<StudentAttendanceDetail> getAttendanceStatus(Student student, Date startDate, Date endDate) {
		
		List<StudentAttendanceDetail> studentList = new LinkedList<StudentAttendanceDetail>();
		
		HashMap<String,Object > map = new HashMap<String, Object>();
		map.put("studentId",student.getReferenceId());
		map.put("startDate",startDate);
		map.put("endDate", endDate);
		
		try {
			@SuppressWarnings("rawtypes")
			List<HashMap> attendanceList = (List<HashMap>)sqlMapClient.queryForList("getAttendanceStatus", map);
			
			SimpleDateFormat sdf = new SimpleDateFormat();
			
			for(@SuppressWarnings("rawtypes") HashMap statusMap : attendanceList) {
				StudentAttendanceDetail attendanceDetail = new StudentAttendanceDetail();
				ClassHours classHours =new ClassHours();
				
				attendanceDetail.setStudentId(((Long) statusMap.get("student_id")).intValue());
				attendanceDetail.setDate((Date)statusMap.get("date"));
				attendanceDetail.setStatus((String) statusMap.get("status"));
				classHours.setClassHourId(((Long) statusMap.get("class_hour_id")).intValue());
				classHours.setStartTime((Time) statusMap.get("start_time"));
				classHours.setEndTime((Time) statusMap.get("end_time"));
				
				attendanceDetail.setClassHours(classHours);
								
				studentList.add(attendanceDetail);
			}
		}
		catch (Exception e) {
			log.error("Error caught while retrieving Student details" + e.getMessage(), e);
		}
		
		return studentList;
	}

	/**
	 * This method inserts student records by cleaning up the existing record.
	 * @param attendanceList
	 */
	public void insertAttendanceDetails(List<StudentAttendanceDetail> attendanceList) {
		
		try {
			
			int count = 0;
			
			log.info("Number of student records deleted is " + deleteAttendanceDetails(attendanceList.get(0)));
			
			for(StudentAttendanceDetail studentDetail : attendanceList) {
				sqlMapClient.insert("insertAttendanceDetails", studentDetail);
			}
			
			log.info("Number of records written " + count);
			
		} catch (SQLException e) {
			log.error("Error caught while writing attendance details" + e.getMessage(), e);
		}
	}
	
	/**
	 * Note this method deletes all the record of the class group which the passed student belong to
	 * @param studentDetail
	 * @return
	 */
	public int deleteAttendanceDetails(StudentAttendanceDetail studentDetail) {
		int count = 0;
		
		try {
			count = sqlMapClient.delete("deleteStudentAttendanceRecord", studentDetail);
			log.info("Number of student records deleted is " + count);
		}
		catch(SQLException se) {
			log.error("Error caught while deleting" + se.getMessage());
		}
		
		return count;
	}
	
}
